package com.timschimandle.getitdone;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class TaskDbHelper extends SQLiteOpenHelper {
	
	private Context c;
	
	//database version and name
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "ToDo";
	
	//constants for the table names
	private static final String TBL_PEOPLE = "tbl_people";
	private static final String TBL_TASKS = "tbl_tasks";
	private static final String TBL_TASK_MANAGER = "tbl_task_manager";
	
	//constants for the fields
	
	//shared
	private static final String ID = "id";
	
	//people table (this tracks the to-do items that have been completed and holds new ones)
	private static final String FIRST_NAME = "first_name";
	private static final String LAST_NAME = "last_name";
	private static final String EMAIL = "email";
	private static final String CELL_PHONE = "cell_phone";
	
	//tasks table (this holds the name of the tasks and how many days should be between each task)
	private static final String TITLE = "title";
	private static final String DAY = "day";
	private static final String MONTH = "month";
	private static final String YEAR = "year";
	private static final String PERSON = "person";
	private static final String ACTIVE = "active";
	
	//task manager table (this holds the names of all the possible assignees
	private static final String TASK = "task";
	private static final String DUE_DATE = "due_date";
	
	//getters for the private variables
	public String getTblPeople() {
		return TBL_PEOPLE;
	}

	public String getTblTasks() {
		return TBL_TASKS;
	}

	public String getTblTaskManager() {
		return TBL_TASK_MANAGER;
	}

	public String getId() {
		return ID;
	}

	public String getFirstName() {
		return FIRST_NAME;
	}

	public String getLastName() {
		return LAST_NAME;
	}

	public String getEmail() {
		return EMAIL;
	}

	public String getCellPhone() {
		return CELL_PHONE;
	}

	public String getTitle() {
		return TITLE;
	}

	public String getDay() {
		return DAY;
	}

	public String getMonth() {
		return MONTH;
	}

	public String getYear() {
		return YEAR;
	}

	public String getPerson() {
		return PERSON;
	}

	public String getActive() {
		return ACTIVE;
	}

	public String getTask() {
		return TASK;
	}

	public String getDueDate() {
		return DUE_DATE;
	}
	//table create statements
	//the people table will hold integers for name and assignee that need to reference the other tables
	private static final String TBL_PEOPLE_CREATE = "CREATE TABLE " + TBL_PEOPLE + "("
            + ID + " INTEGER PRIMARY KEY," 
			+ FIRST_NAME + " TEXT,"
            + LAST_NAME + " TEXT," 
			+ EMAIL + " TEXT," 
            + CELL_PHONE + " TEXT)";
	
	

	private static final String TBL_TASK_CREATE = "CREATE TABLE " + TBL_TASKS + "("
            + ID + " INTEGER PRIMARY KEY," 
			+ TITLE + " TEXT,"
			+ DAY + " INTEGER,"
			+ MONTH + " INTEGER,"
            + YEAR + " INTEGER,"
            + PERSON + " INTEGER,"
            + ACTIVE + " INTEGER)"; //this is a boolean field that will take a 0 or one.
	
	private static final String TBL_TASK_MANAGER_CREATE = "CREATE TABLE " + TBL_TASK_MANAGER + "("
            + ID + " INTEGER PRIMARY KEY," 
			+ TASK + " INTEGER,"
            + DUE_DATE + " NUMERIC)"; 

	TaskDbHelper(Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.c = context;
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(TBL_PEOPLE_CREATE);
		db.execSQL(TBL_TASK_CREATE);
		db.execSQL(TBL_TASK_MANAGER_CREATE);
		
		//indicate the database has been created
		Toast.makeText(c, R.string.database_created, Toast.LENGTH_LONG).show();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub	
	}	
}
